package com.itlong.cloud.operate.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.operate.OperateGroupGetPageDTO;
import com.itlong.cloud.POJO.PO.OperateGroupPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.AssessSyncDataTypeEnum;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateGroupDao;
import com.itlong.cloud.operate.service.IOperateGroupService;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      组织业务接口实现类
 * </desc>
 *
 * @createDate 2017/9/14
 */
@Service
public class OperateGroupServiceImpl implements IOperateGroupService{
    private static final Logger LOG = LoggerFactory.getLogger(OperateGroupServiceImpl.class);

    @Autowired
    IOperateGroupDao iOperateGroupDao;

    @Autowired
    MessageSender messageSender;
    /**
     * <desc>
     *      保存组织
     * </desc>
     *
     * @param operateGroupPO
     * @return
     * @author Jiaqi.X
     * @version 1.0
     * @createDate 2017/09/14
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(OperateGroupPO operateGroupPO) throws Exception {
        operateGroupPO.setGroupId(LogicIdUtil.bussinessId());
        operateGroupPO.setStatus(DataStatusEnum.NORMAL.getType());
        operateGroupPO.setCreateTime(new Date());
        Integer result = iOperateGroupDao.save(SqlUtil.durableData(operateGroupPO, PlatformConstants.TABLE_SAVE));
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", operateGroupPO.getGroupId());
        params.put("groupNum", operateGroupPO.getGroupNum());
        params.put("groupName", operateGroupPO.getGroupName());
        params.put("description", operateGroupPO.getDescription());
        //同步类型 1保存
        params.put("syncType", 1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", params);
        jsonObject.put("type", AssessSyncDataTypeEnum.GROUP.getType());
        messageSender.send(RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_QUEUE, jsonObject.toJSONString(),
                RabbitMQConstant.RABBITMQ_SYNC_DATA_EXCHANGE, RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_KEY);
        return result;
    }

    /**
     * <desc>
     *      更新组织
     * </desc>
     *
     * @param operateGroupPO
     * @return
     * @author Jiaqi.X
     * @version 1.0
     * @createDate 2017/09/14
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(OperateGroupPO operateGroupPO) throws Exception {
        operateGroupPO.setUpdateTime(new Date());
        Integer result = iOperateGroupDao.update(SqlUtil.durableData(operateGroupPO, PlatformConstants.TABLE_UPDATE));
        Map<String, Object> params = new HashMap<>();
        params.put("groupName", operateGroupPO.getGroupName());
        params.put("groupId", operateGroupPO.getGroupId());
        //同步类型2更新
        params.put("syncType", 2);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", params);
        jsonObject.put("type", AssessSyncDataTypeEnum.GROUP.getType());
        messageSender.send(RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_QUEUE, jsonObject.toJSONString(),
            RabbitMQConstant.RABBITMQ_SYNC_DATA_EXCHANGE, RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_KEY);
        return result;
    }


    /**
     * <desc>
     *      获取所有组织
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @version 1.0
     * @createDate 2017/09/14
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateGroupPO> getList() {
        return iOperateGroupDao.getList();
    }

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateGroupPO> getPage(OperateGroupGetPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateGroupPO> page=new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateGroupDao.getPage(params), iOperateGroupDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      根据id查询组织
     * </desc>
     *
     * @param operateGroupPO 组织实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/27
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateGroupPO getOne(OperateGroupPO operateGroupPO) {
        return iOperateGroupDao.getOne(operateGroupPO);
    }

    /**
     * <desc>
     *      将人员分配至部门
     * </desc>
     *
     * @param groupId 部门id
     * @param allotUsers 待分配的用户id 多个，号分隔
     * @param userIds 当前列表所有用户id 多个，号分隔
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/10
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveGroupUsers(String groupId, String allotUsers, String userIds) throws Exception{
        //先将用户的角色删除
        iOperateGroupDao.deleteGroupUsers(StringHandlerUtil.splitString(userIds), groupId);
        //为用户分配角色
        if(StringUtils.isBlank(allotUsers)) {
            return 1;
        }
        Map<String,Object> params=new HashMap<>();
        params.put("allotUsers", StringHandlerUtil.splitString(allotUsers));
        params.put("groupId",groupId);
        params.put("updateTime",new Date());
        params.put("userIds", userIds);
        iOperateGroupDao.saveGroupUsers(params);
        JSONObject jsonObject = new JSONObject();
        params.put("allotUsers", allotUsers);
        jsonObject.put("data", params);
        jsonObject.put("type", AssessSyncDataTypeEnum.USERGROUP.getType());
        messageSender.send(RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_QUEUE, jsonObject.toJSONString(),
                RabbitMQConstant.RABBITMQ_SYNC_DATA_EXCHANGE, RabbitMQConstant.RABBITMQ_ASSESS_SYNC_DATA_KEY);
        return 1;
    }
}
